//
//  NSDate+TimeManager.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/7.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "NSDate+TimeManager.h"

static NSDateFormatter *shareFormatter;

@implementation NSDate (TimeManager)

+ (NSDateFormatter *)formatterWithString:(NSString *)string {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareFormatter = [NSDateFormatter new];
    });
    shareFormatter.dateFormat = string;
    return shareFormatter;
}


#pragma mark - NSDate Change To NSString

- (NSString *)stringMMddFromDate {
    return [[NSDate formatterWithString:D_MMdd] stringFromDate:self];
}

- (NSString *)stringHHmmFromDate {
    return [[NSDate formatterWithString:D_HHmm] stringFromDate:self];
}

- (NSString *)stringHHmmssFromDate {
    return [[NSDate formatterWithString:D_HHmmss] stringFromDate:self];
}

- (NSString *)stringyyyyMMddFromDate {
    return [[NSDate formatterWithString:D_yyyyMMdd] stringFromDate:self];
}

- (NSString *)stringyyyyMMddHHmmFromDate {
    return [[NSDate formatterWithString:D_yyyyMMddHHmm] stringFromDate:self];
}

- (NSString *)stringyyyyMMddHHmmssFromDate {
    return [[NSDate formatterWithString:D_yyyyMMddHHmmss] stringFromDate:self];
}


#pragma mark - NSString Change To NSDate

+ (NSDate *)dateMMddFromString:(NSString *)string {
    return [[NSDate formatterWithString:D_MMdd] dateFromString:string];
}

+ (NSDate *)dateHHmmFromgString:(NSString *)string {
    return [[NSDate formatterWithString:D_HHmm] dateFromString:string];
}

+ (NSDate *)dateHHmmssFromString:(NSString *)string {
    return [[NSDate formatterWithString:D_HHmmss] dateFromString:string];
}

+ (NSDate *)dateyyyyMMddFromString:(NSString *)string {
    return [[NSDate formatterWithString:D_yyyyMMdd] dateFromString:string];
}

+ (NSDate *)dateyyyyMMddHHmmFromString:(NSString *)string {
    return [[NSDate formatterWithString:D_yyyyMMddHHmm] dateFromString:string];
}

+ (NSDate *)dateyyyyMMddHHmmssFromString:(NSString *)string {
    return [[NSDate formatterWithString:D_yyyyMMddHHmmss] dateFromString:string];
}


#pragma mark - NSDate -> NSString For "format"

- (NSString *)customDateFormatterWithString:(NSString *)format {
    return [[NSDate formatterWithString:format] stringFromDate:self];
}

#pragma mark - Birthday Date Change To Age

- (NSInteger)age {
    
    NSTimeInterval dateDiff = [self timeIntervalSinceNow];
    NSInteger age = trunc(dateDiff/D_DAY)/365;
    return ABS(age);
}

+ (NSInteger)ageWithBirthday:(NSDate *)birthday {
    
    NSTimeInterval dateDiff = [birthday timeIntervalSinceNow];
    NSInteger age =trunc(dateDiff/D_DAY)/365;
    return age;
}

#pragma mark - Next Day

+ (NSDate *)nextDay {
    
    NSDate * date = [NSDate date];
    NSDate *nextDay = [NSDate dateWithTimeInterval:D_DAY sinceDate:date];//后一天
    return nextDay;
}


#pragma mark - Last Day

+ (NSDate *)lastDay {
    
    NSDate * date = [NSDate date];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-D_DAY sinceDate:date];//前一天
    return lastDay;
}

@end
